import 'dart:async';
import 'package:flutter/material.dart';

/// https://github.com/iampawan/Flutter-UI-Kit/blob/master/lib/ui/widgets/common_dialogs.dart

///
/// 对话框操作
///
enum DialogAction {
  ok, // 确定
  cancel, // 取消
  yes, // 是
  no, // 否
}

///
/// 提示信息
///
showMessage(BuildContext context, String message) {
  showDialog(
    context: context,
    builder: (context) {
      return AlertDialog(
        title: Text("信息"),
        content: Text(message),
        actions: <Widget>[
          FlatButton(
            child: Text("确定"),
            onPressed: () => Navigator.pop(context),
          ),
        ],
      );
    },
  );
}

///
/// 显示成功信息
///
showSuccess(BuildContext context, String message,
    {IconData icon = Icons.check}) {
  showDialog(
    context: context,
    builder: (context) {
      return Center(
        child: Material(
          borderRadius: BorderRadius.circular(8.0),
          color: Colors.black,
          elevation: 5.0,
          child: Padding(
            padding: const EdgeInsets.all(32.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Icon(icon, color: Colors.green),
                SizedBox(height: 10.0),
                Text(
                  message,
                  style: TextStyle(color: Colors.white),
                ),
              ],
            ),
          ),
        ),
      );
    },
  );
}

///
/// 显示进度
///
showProgress(BuildContext context) {
  showDialog(
    context: context,
    barrierDismissible: false,
    builder: (context) {
      return Center(
        child: CircularProgressIndicator(
          backgroundColor: Colors.yellow,
        ),
      );
    },
  );
}

///
/// 隐藏进度
///
hideProgress(BuildContext context) {
  Navigator.pop(context);
}

///
/// 确认对话框
/// message 确认的信息
///
Future<DialogAction> confirm(BuildContext context, String message) {
  return showDialog<DialogAction>(
    context: context,
    builder: (context) {
      return AlertDialog(
        content: new Text(
          message,
          style: TextStyle(color: Colors.white),
        ),
        actions: <Widget>[
          new FlatButton(
            child: const Text('确定'),
            onPressed: () {
              Navigator.pop(context, DialogAction.ok);
            },
          ),
          new FlatButton(
            child: const Text('取消'),
            onPressed: () {
              Navigator.pop(context, DialogAction.cancel);
            },
          ),
        ],
      );
    },
  );
}

///
/// 选择对话框
///
Future<T> showSelectDialog<T>({
  BuildContext context,
  String title,
  List<DDDialogOption> items,
  T initialValue,
  Function callback,
}) {
  List<Widget> widgetList = List.generate(
    items.length,
    (int index) {
      var value = items[index].value;
      return DDDialogOption<T>(
        icon: items[index].icon ?? Icons.chevron_right,
        color: (initialValue == value) ? Colors.blue : Colors.black,
        text: items[index].text,
        onPressed: () {
          if (value == null) {
            value = items[index].text;
          }
          Navigator.pop(context, value);
        },
      );
    },
  );
  return showDialog<T>(
    context: context,
    builder: (BuildContext context) {
      new SimpleDialog(
        title: Text(title),
        children: widgetList,
      );
    },
  ).then<void>((T value) {
    if (callback != null) {
      callback(value);
    }
  });
}

///
/// 对话框选项
///
class DDDialogOption<T> extends StatelessWidget {
  final IconData icon;
  final Color color;
  final String text;
  final T value;
  final VoidCallback onPressed;

  const DDDialogOption({
    Key key,
    this.icon,
    this.color,
    this.text,
    this.value,
    this.onPressed,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return new SimpleDialogOption(
      onPressed: onPressed,
      child: new Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          new Icon(icon, size: 36.0, color: color),
          new Padding(
            padding: const EdgeInsets.only(left: 16.0),
            child: new Text(text),
          ),
        ],
      ),
    );
  }
}
